<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Isdk extends CI_Controller {
	public function index(){

		echo 111;
	}
	
	public function get_str(){
		$siteid = $this->input->post('siteid');
		$key = $this->get_encrypt($siteid);		
		$data = $this->input->post('the_data');		
		$data = str_replace(' ', '+', $data);			
		$str = $this->encrypt->decode($data, $key);
		return json_decode($str);
	}
	
	///返回密钥
	public function site_encrypt(){
		$siteid = $this->input->post('siteid');
		
		$this->load->model('website_model', 'wm');		
		echo $this->wm->getencrypt($siteid);		
	}
	
	function get_encrypt($siteid){
		$this->load->model('website_model', 'wm');
		
		return $this->wm->getencrypt($siteid);
	}
	//=======================================会员=============================//
	//*************获取用户地址********************//
	public function get_address(){
		$sdk_address = $this->get_str();
		
		$this->load->model('member_model', 'mm');
		
		$query = $this->mm->get_member_address($sdk_address);
		
		echo json_encode($query->first_row());
	}
	
        public function order_status(){
                $sdk_address = $this->get_str();
                //var_dump($sdk_address);die(0);
		$this->load->model('member_model', 'mm');
		$this->load->library('myclass');
		
		$orderid = $sdk_address->ordersn;
                
                $this->load->model('order_model', 'om');
		$orderinfo = $this->om->order_info2($sdk_address);
                
		//$this->load->model('email_model','em');
		//$strings = $this->em->temp_e_list('forgetpwd');
	//	if($strings){
			$q = $this->db->get_where('sdk_site',array('siteid'=>$sdk_address->siteid));
			$siteinfo = $q->row();
                        $succ = $sdk_address->succ;
                        if($succ>0){
                            $tmplateInfo='Dear $username,<br/><br/>Thanks for placing the order on $site.<br/>
Your order number is $orderid. <br/>
Once your order is shipped, we will send you an email with the tracking information. You can trace your parcel online then. 
<br/><br/>
If you have any further problem, please feel free to contact us via email.<br/>
Have a nice day!<br/>
<br/><br/>
Best wishes,
<br/><br/>
$site
';
                            $tmplateTitle = 'Your Order on $site Has Been Successful';
                        }else{
                            $tmplateInfo='Dear $username,<br/><br/>
We are sorry to inform you that your order didn’t go through on $site.<br/>
You can click on the following link and attempt to purchase your basket contents again:<br/>
http://watch.meicii.com/user_mag/myorder <br/>
Once you succeed, you will get another email with your successful order information.<br/><br/>
Sorry for the inconvenience.<br/>
If you have any further problem, please feel free to contact us via email.<br/>
Have a nice day!<br/><br/>
Best wishes,<br/><br/>
$site
';
                            $tmplateTitle = 'Your Order on $site Is Failed';
                        }
			$sendinfo = str_replace(array('$username','$site','$orderid'), array($sdk_address->email,$siteinfo->sitename,$orderid), $tmplateInfo);
			$subject = str_replace('$site',$siteinfo->sitename,$tmplateTitle);
			//获取邮件配置
			$q = $this->mm->get_siteinfo($sdk_address);			
			$this->load->library('myclass');
			$this->myclass->SendMail($subject,$sendinfo,$q->first_row());
	//	}	

		$this->myclass->send_forget_email($q->first_row(), $password);
        }
        //************忘记密码*************************
	public function forget_pwd(){
		$sdk_address = $this->get_str();
                //var_dump($sdk_address);die(0);
		$this->load->model('member_model', 'mm');
		$this->load->library('myclass');
		
		$password = $this->myclass->randomkeys(8);
		$this->mm->forget_password($sdk_address, md5($password));
		
		//$this->load->model('email_model','em');
		//$strings = $this->em->temp_e_list('forgetpwd');
	//	if($strings){
			$q = $this->db->get_where('sdk_site',array('siteid'=>$sdk_address->siteid));
			$siteinfo = $q->row();
                        $tmplateInfo = 'Dear $username,<br/><br/>Your new password is $password, please login into our website with it and go on shopping.<br/><br/>Also, please keep it unknown to any others.<br/><br/>Sorry for the inconvenience.<br/><br/>If you have any further problem, please feel free to contact us.<br/><br/>Have a nice day!<br/><br/>Best wishes,<br/><br/>$site';
                        $tmplateTitle = 'Please Login into $site with New Password';
			$sendinfo = str_replace(array('$username','$site','$password'), array($sdk_address->email,$siteinfo->sitename,$password), $tmplateInfo);
			$subject = str_replace('$site',$siteinfo->sitename,$tmplateTitle);
			//获取邮件配置
			$q = $this->mm->get_siteinfo($sdk_address);			
			$this->load->library('myclass');
			$this->myclass->SendMail($subject,$sendinfo,$q->first_row());
	//	}	

		$this->myclass->send_forget_email($q->first_row(), $password);
	}
	
	//************修改地址*******************
	public function update_address(){
		$sdk_address = $this->get_str();
		
		$this->load->model('member_model', 'mm');
		
		$ack = $this->mm->update_member_address($sdk_address);
		
		echo json_encode(array('ack'=>$ack));
	}
	
	//**************修改密码********************//
	public function update_password(){
		$sdk_member = $this->get_str();
		
		$this->load->model('member_model','mm');
		
		$ack = $this->mm->update_member_password($sdk_member);
		
		echo json_encode(array('ack'=>$ack));
	}
	
	public function forget_password(){
		$sdk_member = $this->get_str();
	}
	
	//***********修改用户姓名*******************
	public function update_name(){
		$sdk_name = $this->get_str();
		
		$this->load->model('member_model', 'mm');
		
		$ack = $this->mm->update_member_name($sdk_name);
		
		echo json_encode(array('ack'=>$ack));
	}
	
	//****************会员登录************************//
	public function user_login(){
		$sdk_login = $this->get_str();

		$this->load->model('member_model', 'mm');
	
		$query = $this->mm->member_login($sdk_login);
		
		$sdk_member = null;
		
		$sdk_member->ack = -1;
		
		if ($query->num_rows() > 0){
			$sdk_member = $query->first_row();
			$sdk_member->ack = 0;
		}

		echo json_encode($sdk_member);
		//ack 0 success, -1 fulat
	}
	
	//*************注册用户**************
	public function register_user(){
	
		$sdk_member = $this->get_str();
                //echo json_encode($sdk_member);
                //die(0);
		
		$this->load->model('member_model','mm');

		$ack = 0;
		if ($this->mm->is_exist_member($sdk_member->siteid, $sdk_member->email))
		{ 	
			$ack = -1; 			
		}else{
			$ack = $this->mm->insert_member($sdk_member);
		}
		
		$uid = 0;
		if ($ack > 0){
			$uid = $ack;
			$ack = 0;
		}

		if($ack == 0){
			$this->load->model('website_model','wm');
			$siteinfo = $this->wm->get_site_row($sdk_member->siteid);
			$username = $sdk_member->email;
			$site = $siteinfo->sitename;
                        
                        $tmplateInfo='Dear $username,<br/><br/>Thank you very much for registering on our website.<br/>Hope you enjoy shopping with us.<br/>If you have any question, please feel free to contact us via email.<br/><br/>Have a nice day!<br/><br/>Best wishes,<br/><br/>$site';
                        $tmplateTitle = 'Welcome, $username! Thanks for Registering on $site.';
			$sendinfo = str_replace(array('$username','$site'), array($sdk_member->email,$siteinfo->sitename), $tmplateInfo);
			$subject = str_replace(array('$username','$site'), array($sdk_member->email,$siteinfo->sitename),$tmplateTitle);
			//获取邮件配置
			$q = $this->mm->get_siteinfo($sdk_member);			
			$this->load->library('myclass');
			$this->myclass->SendMail($subject,$sendinfo,$q->first_row());
                        /*
			$temp = json_encode($tempinfo);						//替换参数
			$confinfo['smtpserver'] = $siteinfo->smtpserver;
			$confinfo['smtpuser'] = $siteinfo->smtpuser;
			$confinfo['smtppassword'] = $siteinfo->smtppassword;
			$confinfo['email'] = $sdk_member->email;
			$conf = json_encode($confinfo);						//配置参数
			$this->load->model('email_model','em');
			$this->em->insert_emailstatus('regester',$temp,$conf);
                         * 
                         */
		}
		
		$sdk_member->mid = $uid;
		$sdk_member->ack = $ack;
		
		echo json_encode($sdk_member);

		//ack = 0 成功 ， -1 已存在， -2 插入失败 
	}
	//============================================会员结束====================================//
	
	public function send_email()
        {
            $this->load->model('website_model','wm');
			//$siteinfo = $this->wm->get_site_row($sdk_order->siteid);
			$tempinfo['site'] = "sssss";
			//$this->load->model('member_model','mm');
			//$memberinfo = $this->mm->get_member_info("mid='$sdk_order->uid'");
			
			$tempinfo['sitename'] = "ssssss";
			$tempinfo['question'] = "";
			$temp = json_encode($tempinfo);
			$confinfo['smtpserver'] = 'ssl://smtp.exmail.qq.com';
			$confinfo['smtpuser'] = 'xyphp@souquick.com';
			$confinfo['smtppassword'] = 'php123456';
			$confinfo['email'] = 'yanzhenfd@163.com';
			$conf = json_encode($confinfo);
                        $conf_obj = json_decode($conf);
		     $this->load->library('myclass');
                     $this->myclass->SendMail('have new question!',"欢迎您的到来",$conf_obj);
        }
	//============================================Question===================================//
	public function add_question()
	{
		$sdk_question = $this->get_str();
		if($sdk_question->question==''){
			echo json_encode(array('ack'=>'-1'));
			exit;
		}			

		$this->load->model('question_model', 'qm');
		$qid = $this->qm->get_question($sdk_question->email,$sdk_question->siteid);
		$questioninfo['questions'] = $sdk_question->question;
		$questioninfo['create_time'] = time();
		
		if($qid){
			$questioninfo['qid'] = $qid;
			$ack = $this->qm->update_question($qid,$questioninfo);				
		}else{
			$question['uid'] = $sdk_question->uid;
			$question['siteid'] = $sdk_question->siteid;
			$question['email'] = $sdk_question->email;
			$question['create_time'] = time();
			$ack = $this->qm->insert_question($question,$questioninfo);
		}
		
		if($ack==0){
			$this->load->model('website_model','wm');
			$siteinfo = $this->wm->get_site_row($sdk_order->siteid);
			$tempinfo['site'] = $siteinfo->sitename;
			$this->load->model('member_model','mm');
			$memberinfo = $this->mm->get_member_info("mid='$sdk_order->uid'");
			$tempinfo['email'] = $sdk_question->email;
			$tempinfo['sitename'] = $siteinfo->sitename;
			$tempinfo['question'] = $sdk_question->question;
			$temp = json_encode($tempinfo);
			$confinfo['smtpserver'] = 'ssl://smtp.exmail.qq.com';
			$confinfo['smtpuser'] = 'xyphp@souquick.com';
			$confinfo['smtppassword'] = 'php123456';
			$confinfo['email'] = 'yanzhenfd@163.com';
			$conf = json_encode($confinfo);
				
			$this->load->model('email_model','em');
			$this->em->insert_emailstatus('question',$temp,$conf);
		}
		
		/* //有订单时发送邮件至yukj@souquick.com
		$this->load->library('myclass');
		$conf->email = 'yukj@souquick.com';
		$conf->smtpserver = 'ssl://smtp.exmail.qq.com';
		$conf->smtpuser = 'root@souquick.com';
		$conf->smtppassword = 'Sdk123$%^';
		
		$this->load->model('website_model','wm');
		$siteinfo = $this->wm->get_site_row($sdk_question->siteid);
		$msg = 'email:'.$sdk_question->email;
		$msg .= 'sitename:'.$siteinfo->sitename.'<br/>';

		$msg .= 'content:'.$sdk_question->content.'<br/>';
		$this->myclass->SendMail('have new question!',$msg,$conf);*/
		
		echo json_encode(array('ack'=>$ack));
	}
	
	public function get_question_list()
	{
		$sdk_question = $this->get_str();
		
		$this->load->model('question_model', 'qm');
		$res = $this->qm->get_site_question_list($sdk_question->uid);
		
		echo json_encode($res);
	}
	//============================================Question End==============================//
	
	//=======================订单=================================
	public function add_order(){
		$sdk_order = $this->get_str();
		
		$this->load->model('order_model', 'om');
		$orderid = $this->om->insert_order($sdk_order);
		$ack = $orderid > 0 ? 0 : -1;
		
		//有订单时发送邮件至yukj@yilufacai.com
	if($ack==0){
			
			$this->load->model('website_model','wm');
			$siteinfo = $this->wm->get_site_row($sdk_order->siteid);
			$tempinfo['site'] = $siteinfo->sitename;
			$this->load->model('member_model','mm');
			$memberinfo = $this->mm->get_member_info("mid='$sdk_order->uid'");
			$tempinfo['firstname'] = $memberinfo->firstname;
			$tempinfo['lastname'] = $memberinfo->lastname;
			$tempinfo['orderid'] = $orderid;
			$tempinfo['goodslist'] = '';
			foreach ($sdk_order->goodslist as $k=>$v){
				$tempinfo['goodslist'] .= 'productname:'.$v->product_name.'<br/>';
				$tempinfo['goodslist'] .= 'price:'.$v->price.$sdk_order->currency;
				$tempinfo['goodslist'] .= 'qty:'.$v->qty.'<br/>';
			}
			$temp = json_encode($tempinfo);
			$confinfo['smtpserver'] = 'ssl://smtp.exmail.qq.com';
			$confinfo['smtpuser'] = 'xyphp@souquick.com';
			$confinfo['smtppassword'] = 'php123456';
			$confinfo['email'] = 'yanzhenfd@163.com';
			$conf = json_encode($confinfo);
			
			$this->load->model('email_model','em');
			$this->em->insert_emailstatus('addorder',$temp,$conf);
		}
		
		echo json_encode(array('ack'=>$ack, 'orderid'=>$orderid));
	}
	
	public function add_spread_order(){
		$sdk_order = $this->get_str();
		
		$this->load->model('spreaduser_model', 'sm');
		$ack = $this->sm->insert_spread_order($sdk_order);
		
		echo json_encode(array('ack'=>$ack)); 
	}
	
	public function update_order_status(){
		$sdk_order = $this->get_str();

		$this->load->model('order_model', 'om');
		$ack = $this->om->order_status2($sdk_order);		
		$orderinfo = $this->om->order_info2($sdk_order);
		$this->load->model('member_model','mm');
		$memberinfo = $this->mm->get_member_info("mid='$orderinfo->uid'");
		$this->load->model('website_model','wm');
		$siteinfo = $this->wm->get_site_row($orderinfo->siteid);
		if($sdk_order->status == 'Success'){			
			$tempinfo['username'] = $memberinfo->email;
			$tempinfo['orderid'] = $sdk_order->ordersn;
			$tempinfo['site'] = $siteinfo->sitename;
			$temp = json_encode($tempinfo);						//替换参数
			$confinfo['smtpserver'] = $siteinfo->smtpserver;
			$confinfo['smtpuser'] = $siteinfo->smtpuser;
			$confinfo['smtppassword'] = $siteinfo->smtppassword;
			$confinfo['email'] = $memberinfo->email;
			$conf = json_encode($confinfo);						//配置参数
			$this->load->model('email_model','em');
			$this->em->insert_emailstatus('notice',$temp,$conf);
		}else{
			$this->load->model('website_model','wm');
			$siteinfo = $this->wm->get_site_row($sdk_order->siteid);
			$tempinfo['username'] = $memberinfo->email;
			$tempinfo['site'] = $siteinfo->sitename;
			$temp = json_encode($tempinfo);						//替换参数
			$confinfo['smtpserver'] = $siteinfo->smtpserver;
			$confinfo['smtpuser'] = $siteinfo->smtpuser;
			$confinfo['smtppassword'] = $siteinfo->smtppassword;
			$confinfo['email'] = $memberinfo->email;
			$conf = json_encode($confinfo);						//配置参数
			$this->load->model('email_model','em');
			$this->em->insert_emailstatus('fnotice',$temp,$conf);
		}
		
		$tempinfo['firstname'] = $memberinfo->firstname;
		$tempinfo['lastname'] = $memberinfo->lastname;
		$tempinfo['orderid'] = $sdk_order->ordersn;
		$tempinfo['orderstatus'] = $sdk_order->status;
		$temp = json_encode($tempinfo);
		$confinfo['smtpserver'] = 'ssl://smtp.exmail.qq.com';
		$confinfo['smtpuser'] = 'root@yilufacai.com';
		$confinfo['smtppassword'] = 'Fc147258@';
		$confinfo['email'] = 'yukj@yilufacai.com';
		$conf = json_encode($confinfo);			
		$this->load->model('email_model','em');
		$this->em->insert_emailstatus('changestatus',$temp,$conf);		
		
		echo json_encode(array('ack'=>$ack));
	}
	
	
	public function order_list(){
		$sdk_order = $this->get_str();
		
		$this->load->model('order_model', 'om');
		$q = $this->om->order_list($sdk_order);
		
		$page = $this->om->order_count($sdk_order) % $sdk_order->pagesize == 0 ? $this->om->order_count($sdk_order) / $sdk_order->pagesize : intval($this->om->order_count($sdk_order) / $sdk_order->pagesize) + 1;
		
		$re_arr = array(
			'page' => $page,
			'order' => $q->result()
		);
		
		echo json_encode($re_arr);
	}
	
	public function product_list()
	{
		$sdk_order = $this->get_str();
		$this->load->model('order_model', 'om');
		
		echo json_encode($this->om->order_products($sdk_order));
	}
	
	public function order_info(){
		$sdk_order = $this->get_str();
		$this->load->model('order_model', 'om');
		$a['orderinfo'] = $this->om->order_info($sdk_order);
		$a['productlist'] = $this->om->order_products($sdk_order);
	
		echo json_encode($a);
	}
	//======================订单结束===============================
	
	//====================货运管理=================================
	public	function shiplist(){
		$sdk_ship = $this->get_str();
		$this->load->model('ship_model', 'sm');
		$q = $this->sm->ship_list();
		
		echo json_encode($q->result());
	}
	//====================货运管理结束=============================
	
	//====================支付返回=================================
	public	function get_url(){
		$orderid = $this->input->post('orderid');
		$this->load->model('return_model', 'rm');
		$q = 'http://www.'.$this->rm->get_url($orderid);
	
		echo $q;
	}
	//====================支付返回结束=============================
	
	//====================优惠劵管理=================================
	public function usediscount(){
	
		$sdk_discount = $this->get_str();
		$this->load->model('discount_model','dm');
		$re = $this->dm->get_discount($sdk_discount->discount_code);
		if($re){
			echo json_encode($re->discount_price);
		}else{
			echo json_encode(-1);
		}
}
}